function gettreecode(){
    var treename=localStorage.getItem("treename");
    console.log("----------------------")
    console.log(treename);
    console.log("----------------------")
    $("#courseclass").val(treename);
}
layui.use(['upload', 'element', 'layer','form'], function() {
    var upload = layui.upload
        , element = layui.element
        , layer = layui.layer,
        form=layui.form;
    form.on('submit(formDemo)', function(data){
        console.log(data.field);
        $.ajax({
            type: "post",
            url: "/advanced/addcoursetable",
            data:data.field,
            success:function(res){
                if(res.state==200){
                    parent.refresh1();
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                }else{
                    layer.msg("上传失败,视频过短");
                }
            }
        })
    });
    var uploadInst = upload.render({
        elem: '#test1'
        , url: '/tp/upload' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#demo1').attr('src', result); //图片链接（base64）
            });

            element.progress('demo', '0%'); //进度条复位
            layer.msg('上传中', {icon: 16, time: 0});
        }
        , done: function (res) {
            //如果上传失败
            if (res.code > 0) {
                return layer.msg('上传失败');
            }
            $("#piano").val(res.src)
            //上传成功的一些操作
            //……
            $('#demoText').html(''); //置空上传失败的状态
        }
        , error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }
        //进度条
        , progress: function (n, elem, e) {
            element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
            if (n == 100) {
                layer.msg('上传完毕', {icon: 1});
            }
        }
    });
    upload.render({
        elem: '#uploadVideo'
        , url: '/intermediate/upload'
        , accept: 'video',//视频
        before:function (e) {
            var progressLayer = layer.open({
                type: 0,
                title: false,
                closeBtn: 0,
                btn: false,
                content: '<div class="layui-progress layui-progress-big" lay-filter="progress" lay-showpercent="true"><div class="layui-progress-bar"></div></div>'
            })
            var scanTime = 1000;
            var timer = setInterval(function (){
                $.ajax({
                    url: "/intermediate/getprocess",
                    success: function (data) {
                        console.log(data);
                        element.progress('progress', data +'%')
                        if(data  == 100){
                            //进度到100%，注意关闭定时器
                            clearInterval(timer);
                            //关闭弹出层
                            layer.close(progressLayer);
                            layer.msg('保存成功')
                        }
                    },
                    error: function (e) {
                        //关闭定时器
                        clearInterval(timer);
                        //关闭弹出层
                        layer.close(progressLayer);
                        layer.msg("保存失败");
                        layer.closeAll();
                    }
                });
            }, scanTime);
        }
        , done: function (res) {//上传后的回调
                console.log(res.path);
                $("#videoplay").attr("src", res.path);
                $("#video").val(res.path);
                $("#time").val(res.time);
                layer.msg('上传成功');
        }
        , error: function () {
            //请求异常回调
            layer.msg('上传失败');
        }

    })

});

$("#selectadd").click(function () {
    layer.open({
        type: 2,
        title: '添加',
        content: "coursetree.html",
        area: ['80%', '80%'],
        end:function (){
            gettreecode();
        }
    })


})
//删除视频
$("#deleteVideo").click(function () {
    var url = $("#videoplay").attr("src");
    if (url == '') {
        layer.msg("没有视频你删除个锤子", { icon: 5 });
        return;
    }
    //发出请求 删除视频文件
    $.post({
        url: "上传视频Demo.aspx",
        data: {
            "Action": "DeleteVideo",
            "url": url
        },
        success: function (data) {
            if (data == '-1') {
                layer.msg("没有视频你删除个锤子", { icon: 5 });
            } else if (data == '0') {
                layer.msg("删除成功", { icon: 1 });
            }
        },
        error: function (errorMsg) {

        }
    })
    $("#videoplay").attr("src", "");//清空路径
})
